package com.zoweunion.mechanic.controller.base;

import com.zoweunion.mechanic.controller.BaseController;
import com.zoweunion.mechanic.entity.VehicleDataBean;
import com.zoweunion.mechanic.model.ResponseBean;
import com.zoweunion.mechanic.service.base.VehicleDataService;
import com.zoweunion.mechanic.util.ExcelUtils;
import com.zoweunion.mechanic.util.MyException;
import com.zoweunion.mechanic.util.constants.Constants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.text.ParseException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/vehicleData")
public class VehicleDataController extends BaseController {

    @Autowired
    private VehicleDataService vehicleDataService;

    /**
     * 当日数据-上部-数据据统计
     *
     * */
    @PostMapping("/vehicleTodayStatisticsData")
    public ResponseBean vehicleTodayStatisticsData(@RequestBody Map<String, Object> reqmap) throws ParseException {
        logBefore(logger, "今日统计-数据统计");
        Map<String, Object> datamap = new HashMap<>();
        datamap = vehicleDataService.vehicleTodayStatisticsData(reqmap);
        logAfter(logger);
        return new ResponseBean(Constants.CODE_SUCCESS, "操作成功", datamap);
    }

    /**
     * 当日数据-折线图-设备参数折线图
     *
     * */
    @PostMapping("/vehicleTodayDataDetail")
    public  ResponseBean vehicleTodayDataDetail(@RequestBody Map<String, Object> reqmap) throws ParseException {
        logBefore(logger, "今日统计-详情数据折线图");
        Map<String, Object> dataDetailList = new HashMap<>();
        dataDetailList = vehicleDataService.vehicleTodayDataDetail(reqmap);
        logAfter(logger);
        return new ResponseBean(Constants.CODE_SUCCESS, "操作成功", dataDetailList);
    }

    /**
     * 当日数据-折线图（表格）-一段时间内工时油耗平均油耗统计
     *
     * */
    @PostMapping("/vehicleOilData")
    public ResponseBean vehicleOilData(@RequestBody Map<String, Object> reqmap) throws MyException{
        logBefore(logger, "油耗统计");
        List<Map<String, Object>> dataList = vehicleDataService.vehicleData(reqmap);
        logAfter(logger);
        return new ResponseBean(Constants.CODE_SUCCESS, "操作成功", dataList);
    }

    /**
     * 导出-当日数据-折线图（表格）-一段时间内工时油耗平均油耗统计
     *
     * */
    @GetMapping("/exportVehicleOilData")
    public void exportVehicleOilData(@RequestParam Map<String, Object> reqmap, HttpServletResponse response) throws Exception{
        logBefore(logger, "油耗详情导出");
        List<VehicleDataBean> dataListExport = vehicleDataService.exportVehicleData(reqmap);
        logAfter(logger);
        ExcelUtils.exportExcelToTarget(response, "工时油耗详细数据", dataListExport, VehicleDataBean.class);
    }

    /**
     * 油耗/工时-月份统计
     *
     * */
    @PostMapping("/vehicleOilDataMonth")
    public ResponseBean vehicleOilDataMonth(@RequestBody Map<String, Object> reqmap) throws MyException, ParseException {
        logBefore(logger, "月份统计");
        List<Map<String, Object>> dataList = vehicleDataService.vehicleDataMonth(reqmap);
        logAfter(logger);
        return new ResponseBean(Constants.CODE_SUCCESS, "操作成功", dataList);
    }

    /**
     * 折线图-平均油耗-7/30天统计
     *
     * */
    @PostMapping("/vehicleOilAverage")
    public ResponseBean vehicleOilAverage(@RequestBody Map<String, Object> reqmap)  throws MyException{
        logBefore(logger, "月份统计");
        Map<String, Object> dataAverageMap = vehicleDataService.vehicleOilAverage(reqmap);
        logAfter(logger);
        return new ResponseBean(Constants.CODE_SUCCESS, "操作成功", dataAverageMap);
    }
}
